using System;
using System.Reflection;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Configuration;
using System.Collections;
using System.Threading;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using ACM.cmsarea08;
using ACM.cmsarea08.mainconfiguration;



namespace ACM.cmsarea08.Core {
	public class EditorCss :  IHttpAsyncHandler { //IHttpAsyncHandler IHttpHandler
/*
<?xml version="1.0" encoding="UTF-8"?>
<JoackVersion>
	<Version>123</Version>
	<Assemblies>
			<Assembly>
				<name>123</name>
				<version>123</version>
			</Assembly>
	</Assemblies>

</JoackVersion>
*/		
		
       public bool IsReusable { get { return true; } }
      public void ProcessRequest(System.Web.HttpContext contesto)
        {
			// non usato
        }
		
	  public IAsyncResult BeginProcessRequest(HttpContext ctx, AsyncCallback cb, object obj)
	  {
	    AsyncEditorCssState reqState = new AsyncEditorCssState(ctx, cb, obj);
	    AsyncEditorCssRequest ar = new AsyncEditorCssRequest(reqState);
	    ThreadStart ts = new ThreadStart(ar.ProcessRequest);
	    Thread t = new Thread(ts);
	    t.Start();
	    return reqState;
	  }
        
	  public void EndProcessRequest(IAsyncResult ar)
	  {
	    AsyncEditorCssState ars = ar as AsyncEditorCssState;
	    if (ars != null)
	    {
	     // Clean Up
	    }
	  }      
      
	}
	
	public class AsyncEditorCssState : IAsyncResult {
		
		internal HttpContext _ctx;
		internal AsyncCallback _cb;
		internal object _dati;
		private bool _iscompleted = false;
		private ManualResetEvent _callCompleteEvent = null;
		
		public AsyncEditorCssState(HttpContext ctx, AsyncCallback cb, object dati) {
			_ctx = ctx;
			_cb = cb;
			_dati = dati;
			
		}
		internal void CompleteRequest() {
			_iscompleted = true;
			lock(this) {
				if(_callCompleteEvent!=null) {
					_callCompleteEvent.Set();
				}
				 if (_cb != null)
				 	_cb(this);
			}
		}
		public object AsyncState {
			get { return _dati; }
		}
		public bool CompletedSynchronously {
			get { return false; }
		}
		public bool IsCompleted            {
			get { return _iscompleted; }
		}
		public WaitHandle AsyncWaitHandle {
			get {
				lock(this) {
					if(_callCompleteEvent == null) 
						_callCompleteEvent = new ManualResetEvent(false);
					return _callCompleteEvent;
				}
			}
		}



	}
	class AsyncEditorCssRequest
	{
	  private AsyncEditorCssState _asyncRequestState;
	
	  public AsyncEditorCssRequest(AsyncEditorCssState ars)
	  {
	    _asyncRequestState = ars;
	  }
	    
	  public void ProcessRequest()
	  {
	  	const string baseEditorCss = "/editor/css/";
	  	string path = "";
	  	string currentTemplate = ACM.cmsarea08.mainconfiguration.MainConf.CurrentMasterTemplate;
       	System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(_asyncRequestState._ctx.Server.MapPath(currentTemplate+baseEditorCss));
       	
       	if(!di.Exists) {
       		path=string.Empty;
       	} else {
       		System.IO.FileInfo[] fis = di.GetFiles("*.css");
       		if(fis.Length>0)
       			path = currentTemplate + baseEditorCss +fis[0].Name;
       	}
       	if(path==string.Empty) {
			di = new System.IO.DirectoryInfo(_asyncRequestState._ctx.Server.MapPath("/public/"+baseEditorCss));       		
			if(di.Exists) {
	       		System.IO.FileInfo[] fis = di.GetFiles("*.css");
	       		if(fis.Length>0)
	       			path = "/public/"+baseEditorCss +fis[0].Name;
			} else
				path=string.Empty;
       	}
       	
 		_asyncRequestState._ctx.Response.ContentType = "text/html";
 		if(path==String.Empty) {
 			_asyncRequestState._ctx.Response.StatusCode = 404;
 			_asyncRequestState._ctx.Response.End();
 		} else
			_asyncRequestState._ctx.Response.TransmitFile(path);
		//finito.
			
	    _asyncRequestState.CompleteRequest();
	  }
	}
	
}
